#include "eqibutils.h"

EQIBUtils * EQIBUtils::_instance = NULL;

EQIBUtils::EQIBUtils(QObject *parent) :
    QObject(parent)
{
}

void EQIBUtils::loadSetting(){
    //QResource::registerResource("qdarkstyle/style.qrc");
    //QResource::registerResource("resources.qrc");
    QString appDirPath = app->applicationDirPath();
    qDebug() << "AppDir:" << appDirPath;
    settings =new QSettings(appDirPath.append("/config.ini"),QSettings::IniFormat);
}

void EQIBUtils::init(){
    this->loadSetting();
    qDebug() << "Loading style...";
    //app->setStyleSheet(this->readTextFile(QString(":resources/qdarkstyle/style.qss")));
    loadingIcons = new QList<QIcon*>();
    for(int i=0;i<8;i++){
        QString p = QString(":resources/imgs/loading-").append(QString::number(i,10)).append(".png");
        QIcon * icon = new QIcon(p);
        qDebug() << "create qicon " << p << icon << icon->isNull() << icon->availableSizes();
        loadingIcons->append(icon);
    }
}
EQIBUtils::~EQIBUtils(){
    qDebug() << "destroy utils...";
    delete settings;
    for(int i=0;i<this->loadingIcons->count();i++){
        //QIcon * icon =
        delete this->loadingIcons->value(i);
    }
    delete this->loadingIcons;
}
QString EQIBUtils::readTextFile(QString path){
    QFile f(path);
    f.open(QFile::ReadOnly);
    QTextStream in(&f);
    QString content = in.readAll();
    f.close();
    return content;
}

EQIBUtils * EQIBUtils::get(){
    if(!_instance){
        _instance = new EQIBUtils();
    }
    return _instance;
}

QString EQIBUtils::getDefaultPage(){
    if ( settings->contains("init.page") )
        return settings->value("init.page").toString();
    else
        return QString("about:black");
}
